<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Questions extends Backend_Controller {

	protected $_title = 'Pertanyaan';
	protected $_question_id = 0;
	
	public function __construct() {
		parent::__construct();
		if(parent::get_actor_login()!="teacher")
			redirect('login/index/1');
		$this->load->model('school_years_model', 'school_years', true);
		$this->load->model('teachings_model', 'teachings', true);
		$this->load->model('subjects_model', 'subjects', true);
	}
		
	public function index($offset=0, $sort_by='name') {
		
		$data = array(
			'mainview' => 'index',
		) + $this->_get_header_data();
		
		$this->load->view('teacher/template',$data);
		
	}
	
	function _crud_output($output = null) {
        $this->load->view('teacher/template_crud.php',$output);    
    }

	function questions_display(){	
		$crud = new grocery_CRUD();
        $crud->set_model('questions_model');
		$crud->set_theme('flexigrid');
        $crud->set_table('questions');
        $crud->set_subject('Bank Soal');
		$crud->columns('chapter_name','create_date','create_time','qty','jumlah_sekarang','create_ip','last_update_date','information','visibility');
		$crud->display_as('chapter_name','Nama Bab');
		$crud->display_as('create_date','Tanggal Pembuatan');
		$crud->display_as('create_time','Waktu Pembuatan');
		$crud->display_as('qty','Total Soal');
		$crud->display_as('jumlah_sekarang','Jumlah Soal Sekarang');
		$crud->display_as('create_ip','IP');
		$crud->display_as('last_update_date','Tanggal Update');
		$crud->display_as('information','Keterangan');
		$crud->display_as('visibility','Publish Soal');
		$crud->required_fields('chapter_name','qty','maximum_skip');
		$crud->fields('chapter_name','qty','information','visibility','school_year_id','create_date','create_time','create_ip','last_update_date','teacher_id','id');
		
		$crud->change_field_type('visibility','true_false');
		$crud->change_field_type('school_year_id','invisible');
		$crud->change_field_type('create_date','invisible');
		$crud->change_field_type('create_time','invisible');
		$crud->change_field_type('create_ip','invisible');
		$crud->change_field_type('last_update_date','invisible');
		$crud->change_field_type('teacher_id','invisible');
		$crud->change_field_type('id','invisible');
		$crud->unset_delete();
		
		$crud->callback_before_insert(array($this, 'insert_meta_data'));
		$crud->callback_before_update(array($this, 'update_meta_data'));
		
		$crud->callback_add_field('visibility',array($this,'callback_edit_visibility'));
		$crud->callback_edit_field('visibility',array($this,'callback_edit_visibility'));
		$crud->add_action('Mengisi Bank Soal', base_url() . 'assets/images/add_bank_soal.png', 'teacher/questions/questions_detail_display');
		$crud->add_action('Bagikan Bank Soal', base_url() . 'assets/images/copy.png', 'teacher/questions/share_questions_display/edit');
		//$crud->set_rules('qty','Jumlah Soal','integer');
		$crud->set_rules('qty', 'qty','callback_qty_check');
		
        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function insert_meta_data($post_array)
	{
		$rs = $this->school_years->get_active_school_year();
		$post_array['school_year_id'] = $rs[0]['id'];
		$post_array['teacher_id'] = $_SESSION['person_id'];
		$post_array['subject_id'] = $_SESSION['subject_id'];
		$post_array['create_date'] = date('Y-m-d');
		$post_array['create_time'] = date('H:i:s');
		$post_array['create_ip'] = $_SERVER['REMOTE_ADDR'];
		$post_array['last_update_date'] = date('Y-m-d');
		return $post_array;
	}
	
	function update_meta_data($post_array,$pk)
	{
		$post_array['last_update_date'] = date('Y-m-d');
		return $post_array;
	}
	
	public function qty_check($str)
	{
		$this->load->model('questions_detail_model', 'questions_detail', true);
		$question_detail_qty = $this->questions_detail->get_qty($_SESSION['question_id']);
		echo " test = " . $question_detail_qty . " pk = " . $_SESSION['question_id'];
		if( $str < $question_detail_qty ) 
		{
			$this->form_validation->set_message('qty_check', 'Jumlah soal tidak boleh < dari total soal yang sudah ada.');
			return FALSE;
		}
		else
			return true;
	}
	
	function callback_edit_visibility($value){
		$retval = '';
		if($value==0)
			$retval = '<input type="radio" value=0 name="visibility" checked> Tidak <input type="radio" value=1 name="visibility"> Ya';
		else
			$retval = '<input type="radio" value=0 name="visibility"> Tidak <input type="radio" value=1 name="visibility" checked> Ya';
		return $retval;
	}
	
	function questions_detail_display($question_id=0){	
		$crud = new grocery_CRUD();
		$crud->set_theme('flexigrid');
		$crud->set_model('questions_detail_model');
        $crud->set_table('questions_detail');
        $crud->set_subject('Detail Bank Soal');
		
		$chapter_name='';
		if($question_id==0) //no direct access to detail problem
			redirect('teacher/questions/questions_display');
		else{
			$_SESSION['question_id'] = $question_id;
			$this->_question_id = $question_id;
			$this->load->model('questions_model', 'questions', true);
			$chapter_name = $this->questions->get_chapter_name($question_id);
			$question_qty = $this->questions->get_qty($question_id);
			
			$this->load->model('questions_detail_model', 'questions_detail', true);
			$question_detail_qty = $this->questions_detail->get_qty($question_id);
			if($question_detail_qty >= $question_qty)
				$crud->unset_add();
		}
		
		$crud->columns('original_number','problem','option_a','option_b','option_c','option_d','option_e','answer');
		$crud->display_as('original_number','No Soal');
		$crud->display_as('problem','Soal');
		$crud->display_as('option_a','Pilihan A');
		$crud->display_as('option_b','Pilihan B');
		$crud->display_as('option_c','Pilihan C');
		$crud->display_as('option_d','Pilihan D');
		$crud->display_as('option_e','Pilihan E');
		$crud->display_as('anwser','Jawaban');
		 
		$crud->required_fields('problem','option_a','option_b','option_c','option_d','option_e','answer');
		//$crud->set_field_upload('image_problem','assets/uploads/files');
		$crud->fields('original_number','problem','option_a','option_b','option_c','option_d','option_e','answer','question_id');
		
		$crud->change_field_type('question_id','invisible');
		$crud->change_field_type('answer','enum',array('a'=>'A','b'=>'B','c'=>'C','d'=>'D','e'=>'E'));
		$this->_question_id = $question_id;
		
		$crud->callback_before_insert(array($this, 'meta_insert_data_questions_detail'));
		$crud->callback_before_update(array($this, 'meta_update_data_questions_detail'));
		$crud->callback_after_insert(array($this, 'test'));
		
		$crud->callback_add_field('original_number',array($this,'callback_add_original_number'));
		$crud->callback_edit_field('visibility',array($this,'callback_edit_visibility'));
	
		$crud->set_rules('qty','Jumlah Soal','integer');
		$crud->set_rules('maximum_skip','Jumlah Skip','integer');
			
        $output = $crud->render();
		$output->chapter_name = $chapter_name;
        $this->_crud_output($output);	
	}

	function callback_add_original_number(){
		
		$this->load->model('questions_detail_model', 'questions_detail', true);
		$rs = $this->questions_detail->get_latest_number($this->_question_id);
		$latest_number = $rs['original_number'];
		return '<input type="text" value='. ($latest_number + 1).' name="original_number" readonly> *) biarkan kosong untuk melanjutkan nomor berikutnya secara otomatis.';
	}
	
	function meta_insert_data_questions_detail($post_array)
	{
		$this->load->model('questions_detail_model', 'questions_detail', true);
		if($this->_question_id > 0)
			$post_array['question_id'] = $this->_question_id;
		else
			$post_array['question_id'] = 0;
		
		$rs = $this->questions_detail->get_latest_number($this->_question_id);
		$latest_number = $rs['original_number'];
		$post_array['original_number'] = $latest_number + 1;
		echo var_dump($post_array);
		return $post_array;
	}
	
	function meta_update_data_questions_detail($post_array)
	{
		if($this->_question_id > 0)
			$post_array['question_id'] = $this->_question_id;
		else
			$post_array['question_id'] = 0;
		
		echo var_dump($post_array);
		return $post_array;
	}
	
	function share_questions_display($operation="", $question_id=0){
		$crud = new grocery_CRUD();
		$crud->set_theme('flexigrid');
		$crud->set_model('questions_share_model');
        $crud->set_table('teachers');
       
		//$crud->unset_back_to_list();

		if($operation=="" || $question_id==0 || $operation=="add"){ //no direct access to detail problem
			redirect('teacher/questions/questions_display');
		}
		
		$crud->columns('name');
		$crud->callback_column('name',array($this,'_callback_checkbox'));
		$crud->display_as('name','Nama Guru');
		$crud->fields('name');
		$crud->required_fields('name');
		
		$person_id = isset($_SESSION['person_id'])?$_SESSION['person_id']:'none';
		if($person_id != 'none' && $_SESSION['actor']=="teacher") {
			$subject_id = $this->teachings->get_subject_id($person_id);
			$subject_name = $this->subjects->get_name($subject_id);
			$data = $this->questions_share_model->lists_array($subject_id, $question_id);
			
			$crud->change_field_type('name','dropdown',$data);
			$crud->set_subject('Data Guru Bidang Studi - ' . $subject_name);
		}
        $output = $crud->render();
        $this->_crud_output($output);	
	}
	
	function _callback_checkbox($value, $row){
		$retval = anchor('teachers/questions/save_share/'. $row->id,$value);
		//$retval='';
		return $retval;
	}
	
}
